<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 86185
  Date: 2021/5/10
  Time: 10:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //获取项目的根路径
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<html>
<head>
    <title>Title</title>
    <meta charset="utf-8">
    <title>检查缴费</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="<%=basePath%>static/layui-v2.6.5/layui/css/layui.css" media="all">

    <link rel="stylesheet" href="<%=basePath%>static/swee/sweetalert.css">
    <script src="<%=basePath%>static/swee/sweetalert-dev.js"></script>
    <script src="<%=basePath%>static/jquery/jquery-1.12.4.js"></script>
</head>
<body>

<form action="<%=basePath%>cost/index" method="post" style="margin-left: 40%">
    <input type="text" name="name" lay-verify="email" autocomplete="off" class="name">
    <button type="submit" id="select" class="layui-btn layui-btn-radius">查询</button>
</form>

<table class="layui-table" lay-skin="line">
    <colgroup>
        <col width="150">
        <col width="150">
        <col width="200">
        <col>
    </colgroup>
    <thead>
    <tr>
        <th>缴费编号</th>
        <th>诊断号</th>
        <th>用户姓名</th>
        <th>缴费时间</th>
        <th>缴费类型</th>
        <th>缴费状态</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody>
    <c:forEach items="${page.list}" var="list">
        <tr class="tr">
            <td>${list.id}</td>
            <td>${list.userId}</td>
            <td>${list.name}</td>
            <td>${list.userTime}</td>
            <td>${list.type}</td>
            <c:if test="${list.state==0}">
                <td>未缴费</td>
            </c:if>
            <td>
                <button type="button" class="layui-btn  layui-btn-radius" id="cost">确认缴费</button>
            </td>
        </tr>
    </c:forEach>
    </tbody>
</table>
<div style="text-align:center;">
    <a href="<%=basePath%>cost/index?pageNum=${page.prePage}&name=${name}">上一页</a>
    <span>第${page.pageNum}页/共${page.pages}页(${page.total}条记录)</span>
    <a href="<%=basePath%>cost/index?pageNum=${page.nextPage}&name=${name}">下一页</a>
</div>

<script type="text/javascript">
    $(function () {
        $("tbody").on("click", "#cost", function () {
            let id = $(this).parent().parent().children("td:eq(0)").text();  //id
            let userId = $(this).parent().parent().children("td:eq(1)").text();  //用户id
            swal({
                    title: "确定缴费吗？",
                    text: "你将无法撤回此次操作！",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定！",
                    cancelButtonText: "取消！",
                    closeOnConfirm: false,
                    closeOnCancel: false
                },
                function (isConfirm) {
                    if (isConfirm) {
                        $.ajax({
                            url: "<%=basePath%>cost/pay",
                            type: "post",
                            data: {
                                id: id,
                                userId: userId
                            },
                            dataType: "json",
                            success: function (data) {
                                if (data) {
                                    swal({
                                        title: "缴费成功",
                                        text: "本次缴费300元"
                                    }, function () {
                                        window.location.replace("<%=basePath%>cost/index?name=${name}")
                                    })
                                } else {
                                    swal({
                                        title: "余额不足！",
                                        showConfirmButton: false,
                                        showCancelButton: true,
                                        confirmButtonColor: "#DD6B55",
                                        confirmButtonText: "确定！",
                                        closeOnConfirm: false,
                                        closeOnCancel: false
                                    })
                                }
                            }
                        })
                    } else {
                        swal("您选择了取消！", " ", "error");
                    }
                });
        })
    })

</script>
</body>
</html>
